<template>
  <div style="height: 100%">
    <!--引用表格-->
    <BasicTable @register="registerTable" @fetch-success="handleData" :row-selection="rowSelection" style="height: 100%">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined">新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <LabItemsModal ref="registerModal" @success="handleSuccess"></LabItemsModal>
  </div>
</template>

<script lang="ts" name="items-labItems" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns } from './LabItems.data';
  import { list, queryByPid, deleteOne, batchDelete, getImportUrl, getExportUrl } from './LabItems.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import LabItemsModal from './components/LabItemsModalB.vue';

  const queryParam = reactive<any>({
    type: 'B',
    id: '',
  });
  const props:any = defineProps({ change1Id: { type:Object || String } });
  const emit = defineEmits(['changeSelect2']);
  const registerModal = ref();
  const { tableContext } = useListPage({
    tableProps: {
      title: '实验项目信息表',
      api: queryByPid,
      columns,
      canResize: false,
      useSearchForm: false,
      showIndexColumn: true,
      scroll: { y: 600 },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        console.log('props.change1Id',props.change1Id)
        queryParam.id = props.change1Id.id;        
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '实验项目信息表',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });
  const [registerTable, { reload, clearSelectedRowKeys }, { selectedRowKeys }] = tableContext;
  let sourceData = ref([]);
  const handleData = (res) => {
    sourceData.value = res.items;
    emit('changeSelect2', sourceData.value[0]);
  };
  const rowSelection = {
    type: 'radio',
    columnWidth: 50,
    onChange: onSelectChange,
  };
  function onSelectChange(selectedRowKeys: (string | number)[],rowSelection) {
    if (selectedRowKeys[0]) {
      emit('changeSelect2', rowSelection[0]);
    } else {
      if (sourceData.value[0]) {
        emit('changeSelect2', sourceData.value[0]);
      }
    }
  }
  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add(props.change1Id);
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(props.change1Id,record);
  }

  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(props.change1Id,record);
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
    reload();
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  /**
   * 查询
   */
  // function searchQuery() {
  //   reload();
  // }

  /**
   * 重置
   */
  // function searchReset() {
  //   formRef.value.resetFields();
  //   selectedRowKeys.value = [];
  //   reload();
  // }

  /**
   * form点击事件
   * @param value
   */
  // function handleFormChange(key, value) {
  //   queryParam[key] = value;
  // }

  /**
   * 初始化字典配置
   */
  // function initDictConfig() {
  //   loadCategoryData({code:''}).then((res) => {
  //     if (res) {
  //       let allDictDate = getAuthCache(DB_DICT_DATA_KEY);
  //       if(!allDictDate['']){
  //         Object.assign(allDictDate,{'':res})
  //       }
  //       setAuthCache(DB_DICT_DATA_KEY,allDictDate)
  //     }
  //   });
  // }
  defineExpose({
    reload,
    clearSelectedRowKeys,
  });
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust {
      width: calc(50% - 15px);
      min-width: 100px !important;
    }
    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }
</style>
